package com.awspaas.user.apps.shhtaerospaceindustrial.event;

import java.util.List;

import com.actionsoft.bpms.commons.formfile.model.delegate.FormFile;
import com.actionsoft.bpms.dw.design.event.DataWindowFormatDataEventInterface;
import com.actionsoft.bpms.server.SSOUtil;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.server.fs.DCContext;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;



/**   
* @Title: DWDataFormatEvent.java  
* @Description: 
* @author OnlyWjt
* @date 2019年7月29日 下午8:26:52 
* @version V1.0   
**/
public class ProcessDwQueryDataFormatEvent2 implements DataWindowFormatDataEventInterface {
	public static String objToStr(Object obj) {
		return obj == null ? "" : obj.toString();
	}

	@Override
	public void formatData(UserContext uc, JSONArray datas) {
		try {
			for (Object datao : datas) {//获取值
				JSONObject data = (JSONObject) datao;
				String bindId = data.getString("BINDID");
				String taskId = "";
				String queryTaskId = "SELECT MAX(ID) ID FROM WFH_TASK WHERE PROCESSINSTID = '"+bindId+"'  AND DISPATCHID IS NOT NULL";
				taskId = objToStr(DBSql.getString(queryTaskId, "ID"));
				
				String previewDwDelete = "<button type='button' onClick=\"openFormDetail('"+bindId+"','"+taskId+"');\">查看表单</button>";
				data.put("REASON", previewDwDelete);//将查看详情格式化
//				String previewDwUpdate = "<button type='button' onClick=\"print('"+bindId+"','"+taskId+"','"+spzt+"');\">打印</button>";
//				data.put("DY", previewDwUpdate);//将打印格式化
				
				SSOUtil ssoUtil = new SSOUtil();
				String sid = ssoUtil.registerClientSessionNoPassword("admin", "cn", "", "pc");
				String boid = objToStr(data.get("PSNID"));				
				List<FormFile> files = SDK.getBOAPI().getFiles(boid, "ABOUTFILE");// 获取附件的源文件
				
				String portalUrl = SDK.getPlatformAPI().getPortalUrl();
				String FIleUrl = "";
				if(files != null && files.size() > 0) {
					String commonfileview="";
					for(int i=0;i<files.size();i++)
					{
					DCContext sourceDc = SDK.getBOAPI().getFileDCContext(files.get(i));// 获得表单附件的DC上下文
					String downLoadUrl = sourceDc.getDownloadURL();
					//获取图片地址：路径+具体图片名
					FIleUrl = portalUrl + "/r" + downLoadUrl.substring(1, downLoadUrl.length()-1).replace("sid=null", "sid="+sid);
					commonfileview += "<button type='button' onClick=\"openFormFile('"+FIleUrl+"');\">浏览</button>";
					}
					data.put("ABOUTFILE", commonfileview);//将查看详情格式化
					
				}
				

				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
